package com.mz.admin.controller.common;

import com.github.pagehelper.PageHelper;
import com.mz.common.core.domain.AjaxResult;
import com.mz.common.core.domain.entity.SysDictType;
import com.mz.common.core.page.PageDomain;
import com.mz.framework.service.impl.SysDictTypeServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典信息
 *
 * @author zzq
 */
@Tag(name = "SysDictTypeController", description = "数据字典类型管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/common/dict/type")
public class SysDictTypeController {

    private final SysDictTypeServiceImpl dictTypeService;

    /**
     * 查询数据字典类型列表
     */
    @Operation(summary = "查询数据字典类型列表")
    @GetMapping("/list")
    public AjaxResult list(SysDictType dictType, PageDomain pageDomain) {
        PageHelper.startPage(pageDomain.getPageNum(), pageDomain.getPageSize());
        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
        return AjaxResult.successPage(list);
    }

    /**
     * 查询字典类型详细
     */
    @Operation(summary = "查询字典类型详细")
    @GetMapping(value = "/getInfo/{dictId}")
    public AjaxResult getInfo(@PathVariable Long dictId) {
        return AjaxResult.success(dictTypeService.selectDictTypeById(dictId));
    }

    /**
     * 新增字典类型
     */
    @Operation(summary = "新增字典类型")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody SysDictType dict) {
        if (!dictTypeService.checkDictTypeUnique(dict)) {
            return AjaxResult.error("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        return AjaxResult.success(dictTypeService.insertDictType(dict));
    }

    /**
     * 修改字典类型
     */
    @Operation(summary = "修改字典类型")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody SysDictType dict) {
        if (!dictTypeService.checkDictTypeUnique(dict)) {
            return AjaxResult.error("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        return AjaxResult.success(dictTypeService.updateDictType(dict));
    }

    /**
     * 删除字典类型
     */
    @Operation(summary = "删除字典类型")
    @PostMapping("remove/{dictIds}")
    public AjaxResult remove(@PathVariable Long[] dictIds) {
        dictTypeService.deleteDictTypeByIds(dictIds);
        return AjaxResult.success();
    }

    /**
     * 刷新字典缓存
     */
    @Operation(summary = "刷新字典缓存")
    @PostMapping("/refreshCache")
    public AjaxResult refreshCache() {
        dictTypeService.resetDictCache();
        return AjaxResult.success();
    }

    /**
     * 获取字典选择框列表
     */
    @Operation(summary = "获取字典选择框列表")
    @GetMapping("/optionselect")
    public AjaxResult optionselect() {
        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
        return AjaxResult.success(dictTypes);
    }
}
